var gulp        = require('gulp'),
    browserSync = require('browser-sync').create(),
    plugins     = require('gulp-load-plugins')(), 
    reload      = browserSync.reload,
    handleErrors = require('./src/util/handleErrors'); 



gulp.task('sass', function() {
    return gulp.src('src/sass/*.scss')
           .pipe(plugins.sass()) 
           .on('error', handleErrors)
           .pipe(gulp.dest('public/css/')) 
           .pipe(reload({stream: true})); 
});



// 压缩css
gulp.task('compress-css',function(){
    return gulp.src('public/css/*css')  
           .pipe(plugins.cleanCss()) 
           .pipe(gulp.dest('public/css/')); 
})

// 压缩js
gulp.task('compress-js',function(){
    return gulp.src('src/js/*js')
           .pipe(plugins.uglify())
           .on('error', handleErrors)
           .pipe(gulp.dest('public/js/'));
})

//压缩img
gulp.task('compress-img',function(){
    return gulp.src('src/img/*') 
           .pipe(plugins.imagemin()) 
           .pipe(gulp.dest('public/img/'))
})
// 静态服务器 + 监听 scss/html 文件
gulp.task('serve', ['sass','compress-img','compress-js','compress-css'], function() {
    browserSync.init({
               server: {
            baseDir: "./", 
            index:'index.html' 
        },
        port:80     
    });
    gulp.watch("./*.html").on('change', reload); 
    gulp.watch('src/sass/*.scss', ['sass']); 
    gulp.watch('public/css/*.css', ['compress-css']);
    gulp.watch('src/js/*js', ['compress-js']); 
    gulp.watch('src/img/*', ['compress-img']); 
    gulp.watch('public/js/*.js').on('change', reload); 
    gulp.watch("public/css/*.css").on('change', reload); 
});


gulp.task('default', ['serve']);